package pages;

import core.TestBase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import ui_tests.TestData;
import utils.Log4Test;

import java.util.List;

public class NotebooksPage extends TestBase {

    public int count = 0;
    protected By appleManuf = By.xpath("//div[2]/div[3]//li[2]/ul/li");
    protected WebElement productTabState;
    List<WebElement> manufacturesList;
    private String URL = "http://rozetka.com.ua/notebooks/c80004/";
    private String notebookLinkLocator = "//*[@id='computers-notebooks']/div[@class='m-main-i active']";
    protected By tabState = By.xpath(notebookLinkLocator);

    public boolean isOpened() {
        Log4Test.info("Link opened" + webDriver.getCurrentUrl());
        return webDriver.getCurrentUrl().equals(URL);
    }

    public WebElement verifyProductTabState() {

        Log4Test.info("Verify Product Tab state");
        return productTabState = webDriver.findElement(tabState);
    }

    public List<WebElement> findManufactures() {
        manufacturesList = webDriver.findElements(appleManuf);
        Log4Test.info("List of Manufactures created");
        return manufacturesList;
    }

    public int loopThroughManufacturesList() {
        findManufactures();
        for (WebElement elem : manufacturesList) {
            count++;
        }
        Log4Test.info("Count of Manufactures counted =" + count);
        return count;
    }

    public void clickAppleManufacture() {

        for (int i = 1; i <= count; i++) {
            WebElement link = webDriver.findElement(By.xpath("//div[2]/div[3]//li[2]/ul/li[" + i + "]/a"));
            if (link.getText().contains(TestData.manufact_item)) {
                link.click();
                Log4Test.info("Open link Apple");
                break;
            }
        }
    }
}
